﻿Public Class frmTarifs
    Private szSQL As String
    Private rs As ADODB.Recordset
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0

    Dim OLD_REF As String
    Dim szFamille As String

    Private Sub frmTarifs_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' vider le combobox FAMILLE
        CB_FAMILLE.SelectedIndex = 0

        ' vider le combobox TVA
        CB_TVA.Items.Clear()
        ' Lire la table TVA
        szSQL = "CALL spGet_P_TABLES_byCODE('P_TVA',0);"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        Do While Not rs.EOF
            CB_TVA.Items.Add(rs.Fields("code").Value & " | " & rs.Fields("taux").Value & " %")
            rs.MoveNext()
        Loop
        ' Close the recordset AND connection.
        rs.Close()
        CB_TVA.SelectedIndex = 0

        ' utilisateur autorisé à modifier ?
        If gUserAcces < 9 Then
            BO_ENREGISTRER.Enabled = False
        Else
            BO_ENREGISTRER.Enabled = True
        End If

    End Sub

    Private Sub CB_FAMILLE_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CB_FAMILLE.SelectedIndexChanged
        szFamille = Mid(CB_FAMILLE.SelectedItem.ToString, 1, 1)
        InitListeTarifs(0)
    End Sub

    Private Sub InitListeTarifs(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' si la colonne de tri est différente de celle par défaut, retrier la listview sur la 1ere colonne
        If m_SortingIndex > 0 Then
            SortListView(LV_TARIFS, m_SortingColumn, 0)
        End If
        ' effacer la listview
        LV_TARIFS.Items.Clear()
        LV_TARIFS.ListViewItemSorter = Nothing

        ' Lire la table FORMULES
        szSQL = "CALL spGet_TARIFS_byFAM('FAM','" & szFamille & "'," & CInt(chkAVEC_OBSOLETES.Checked) & ")"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        Do While Not rs.EOF
            LV_TARIFS.Items.Add(rs.Fields("ref").Value)
            LV_TARIFS.Items(nb).SubItems.Add(rs.Fields("lib").Value)
            LV_TARIFS.Items(nb).SubItems.Add(rs.Fields("puttc").Value)
            If rs.Fields("obsolete").Value <> 0 Then
                LV_TARIFS.Items(nb).ForeColor = Color.Gray
            End If
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        LB_NbLignes.Text = CStr(nb) & " lignes"

        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            SortListView(LV_TARIFS, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_TARIFS)

    End Sub

    Private Sub BO_Enregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ENREGISTRER.Click

        ' mise à jour 
        szSQL = "CALL spUpd_TARIFS('" & OLD_REF & "','" & TE_REF.Text & "','" & TE_LIB.Text & "','" & Mid(CB_FAM.SelectedItem, 1, 1) & "'," & CB_TVA.SelectedIndex & "," & _
            TE_PUTTC.Value & "," & TE_COMPTE.Value & ",'" & CB_TYPE_EXAMEN.SelectedItem & "'," & CInt(chkOBSOLETE.Checked) & "," & _
            CInt(chkWEB.Checked) & "," & TE_DELAI_RESA.Value & ",'" & gUserCode & "')"
        ExecQuery(ctDSN_AEM, szSQL)
        ' Cacher les détails
        BO_ANNULER_Click(sender, e)

    End Sub

    Private Sub TariftoScreen(ByVal ref As String)

        ' Lire l'enreg sélectionné
        szSQL = "CALL spGet_TARIFS('" & ref & "')"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount > 0 Then
            TE_REF.Text = rs.Fields("ref").Value
            TE_LIB.Text = rs.Fields("lib").Value
            CB_FAM.SelectedIndex = CB_FAM.FindString(rs.Fields("fam").Value.ToString)
            CB_TVA.SelectedIndex = rs.Fields("ctva").Value
            TE_PUTTC.Value = rs.Fields("puttc").Value
            TE_COMPTE.Value = rs.Fields("compte").Value
            CB_TYPE_EXAMEN.SelectedIndex = CB_TYPE_EXAMEN.FindString(rs.Fields("type_examen").Value)
            chkOBSOLETE.Checked = rs.Fields("obsolete").Value
            chkWEB.Checked = rs.Fields("affichage_web").Value
            TE_DELAI_RESA.Value = rs.Fields("delai_reservation").Value
            TE_MODIFIE_LE.Text = setModifiedBy(rs.Fields("cree_le").Value, rs.Fields("modif_le").Value, rs.Fields("cd_uti").Value)
        End If
        rs.Close()

    End Sub

    Private Sub LV_TARIFS_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_TARIFS.ColumnClick
        SortListView(LV_TARIFS, m_SortingColumn, e.Column)
        m_SortingIndex = e.Column
    End Sub

    Private Sub LV_TARIFS_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_TARIFS.DoubleClick

        If LV_TARIFS.SelectedItems.Count > 0 Then
            TariftoScreen(LV_TARIFS.SelectedItems(0).Text)
            ' afficher les détails de la formule
            TC_Details.Visible = True
            ' mémoriser ancien identifiant pour maj
            OLD_REF = TE_REF.Text
        End If

    End Sub

    Private Sub LV_TARIFS_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_TARIFS.KeyDown
        Select Case e.KeyCode
            ' touche Enter = DoubleClick
            Case Keys.Enter
                LV_Tarifs_DoubleClick(sender, e)

            Case Keys.Insert
                BO_ADD_LIGNE_Click(sender, e)

            Case Keys.Delete
                BO_DELETE_LIGNE_Click(sender, e)

        End Select

    End Sub

    Private Sub BO_DELETE_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_DELETE_LIGNE.Click
        ' utilisateur autorisé à supprimer ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        deleteEnregistrement("TARIFS", LV_TARIFS.SelectedItems(0).SubItems(1).Text, LV_TARIFS.SelectedItems(0).Text)
        ' Cacher les détails
        BO_ANNULER_Click(sender, e)
    End Sub

    Private Sub BO_ADD_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        ' utilisateur autorisé à ajouter ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        ' Ajouter un nouveau tarif
        szSQL = "spINS_TARIFS('0000','" & szFamille & "') "
        If ExecQuery(ctDSN_AEM, szSQL) Then
            ' réaffiche la viewlist
            InitListeTarifs(0)
            ' ouvrir le fomulaire de modif
            LV_Tarifs_DoubleClick(sender, e)
        End If
    End Sub

    Private Sub BO_ANNULER_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ANNULER.Click
        ' Cacher les détails
        TC_Details.Visible = False
        ' réaffiche la viewlist
        InitListeTarifs(LV_TARIFS.SelectedItems(0).Index)
    End Sub

    Private Sub chkAVEC_OBSOLETES_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkAVEC_OBSOLETES.CheckedChanged
        Call InitListeTarifs(0)
    End Sub
End Class